﻿
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
namespace Maticsoft.DBUtility
{
    public class EntityHelper
    {
        public static T BuildEntity<T>(SqlDataReader reader) where T: class, new()
        {
            if (reader == null)
            {
                throw new Exception("DataReader object is null!");
            }
            int fieldCount = -1;
            T instance = (T) Activator.CreateInstance(typeof(T));
            while (reader.Read())
            {
                fieldCount = (fieldCount == -1) ? reader.FieldCount : fieldCount;
                for (int i = 0; i < fieldCount; i++)
                {
                    PropertyInfo propInfo = instance.GetType().GetProperty(reader.GetName(i), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (propInfo != null)
                    {
                        if ((reader[i] == null) || Convert.IsDBNull(reader[i]))
                        {
                            propInfo.SetValue(instance, null, null);
                        }
                        else
                        {
                            propInfo.SetValue(instance, reader[i], null);
                        }
                    }
                }
            }
            reader.Close();
            reader.Dispose();
            return instance;
        }

        public static List<T> BuildEntityToList<T>(DataTable dt) where T: class, new()
        {
            if (dt == null)
            {
                throw new Exception("DataTable object is null!");
            }
            List<T> entityList = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                T obj = Activator.CreateInstance<T>();
                PropertyInfo[] pros = typeof(T).GetProperties();
                foreach (DataColumn col in dt.Columns)
                {
                    PropertyInfo propInfo = obj.GetType().GetProperty(col.ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (propInfo != null)
                    {
                        if ((row[col.ColumnName] == null) || Convert.IsDBNull(row[col.ColumnName]))
                        {
                            propInfo.SetValue(obj, null, null);
                        }
                        else
                        {
                            propInfo.SetValue(obj, row[col.ColumnName], null);
                        }
                    }
                }
                entityList.Add(obj);
            }
            return entityList;
        }

        public static List<T> BuildEntityToList<T>(SqlDataReader reader) where T: class, new()
        {
            if (reader == null)
            {
                throw new Exception("DataReader object is null!");
            }
            List<T> entityList = new List<T>();
            int fieldCount = -1;
            while (reader.Read())
            {
                fieldCount = (fieldCount == -1) ? reader.FieldCount : fieldCount;
                T instance = (T) Activator.CreateInstance(typeof(T));
                for (int i = 0; i < fieldCount; i++)
                {
                    PropertyInfo propInfo = instance.GetType().GetProperty(reader.GetName(i), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (propInfo != null)
                    {
                        if ((reader[i] == null) || Convert.IsDBNull(reader[i]))
                        {
                            propInfo.SetValue(instance, null, null);
                        }
                        else
                        {
                            propInfo.SetValue(instance, reader[i], null);
                        }
                    }
                }
                entityList.Add(instance);
            }
            reader.Close();
            reader.Dispose();
            return entityList;
        }
    }
}

